﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Retirement_Admin_changeDetails : System.Web.UI.Page
{

    PolicyRules policyRules = new PolicyRules();
    Customer customer = new Customer();
    Policy policy = new Policy();

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            this.policyNo.Value = this.Request.QueryString["policyNo"];
            String policyNo = this.Request.QueryString["policyNo"];
            Policy policy = Policy.getPolicy(policyNo);

            Customer customer = Customer.getCustomerInfo(policy.nric);

            //Disable TextFields on page load
            this.tb_firstName.Enabled = false;
            this.tb_lastName.Enabled = false;
            this.tb_nric.Enabled = false;
            this.tb_nationality.Enabled = false;
            this.tb_dob.Enabled = false;
            this.rb_male.Enabled = false;
            this.rb_female.Enabled = false;
            this.tb_maritalStatus.Enabled = false;
            this.rb_smoker.Enabled = false;
            this.rb_nonSmoker.Enabled = false;
            this.tb_mailingAddress.Enabled = false;
            this.tb_postalCode.Enabled = false;
            this.tb_mobileNo.Enabled = false;
            this.tb_telNo.Enabled = false;
            this.tb_email.Enabled = false;


            this.tb_occupation.Enabled = false;
            this.tb_jobDuties.Enabled = false;
            this.tb_employer.Enabled = false;
            this.tb_noOfYears.Enabled = false;
            this.tb_industry.Enabled = false;
            this.tb_annualEarnedIncome.Enabled = false;
            this.tb_annualEarnedIncomePrevious.Enabled = false;
            this.rb_yesOtherIncome.Enabled = false;
            this.rb_noOtherIncome.Enabled = false;
            this.tb_otherIncomeSource.Enabled = false;
            this.tb_otherAnnualEarnedIncome.Enabled = false;
            this.tb_sourceOfWealth.Enabled = false;
            this.rb_yesBankruptcy.Enabled = false;
            this.rb_noBankruptcy.Enabled = false;


            this.tb_planName.Enabled = false;
            this.tb_basicSumInsured.Enabled = false;
            this.tb_basicPlanDuration.Enabled = false;
            this.rb_plusRiderYes.Enabled = false;
            this.rb_plusRiderNo.Enabled = false;
            this.tb_plusRiderDuration.Enabled = false;
            this.tb_plusRiderSumInsured.Enabled = false;
            this.rb_waiverRiderYes.Enabled = true;
            this.rb_waiverRiderNo.Enabled = false;
            this.tb_waiverRiderDuration.Enabled = false;
            this.tb_totalAnnualPremium.Enabled = false;


            this.rb_monthly.Enabled = false;
            this.rb_semiAnnually.Enabled = false;
            this.rb_annually.Enabled = false;
            this.rb_auto.Enabled = false;
            this.rb_direct.Enabled = false;
            this.tb_accountNumber.Enabled = false;
            this.tb_accountType.Enabled = false;
            this.tb_currency.Enabled = false;
            

            //Show Details
            this.tb_firstName.Text = customer.firstName;
            this.tb_lastName.Text = customer.lastName;
            this.tb_nric.Text = customer.nric;
            this.tb_nationality.Text = customer.nationality;
            this.tb_dob.Text = String.Format("{0:dd/MM/yyyy}", customer.dob);
            this.tb_maritalStatus.Text = customer.maritalStatus;


            if ("Male".Equals(customer.gender))
            {
                rb_male.Checked = true;
                rb_female.Checked = false;
            }
            else
            {
                rb_male.Checked = false;
                rb_female.Checked = true;
            }

            if ("Y".Equals(customer.smoker))
            {
                rb_smoker.Checked = true;
                rb_nonSmoker.Checked = false;
            }
            else
            {
                rb_smoker.Checked = false;
                rb_nonSmoker.Checked = true;
            }

            this.tb_mailingAddress.Text = customer.mailingAddress;
            this.tb_postalCode.Text = customer.postalCode;
            this.tb_mobileNo.Text = customer.mobileNo;
            this.tb_telNo.Text = customer.telNo;
            this.tb_email.Text = customer.email;

            //Occupational Details
            this.tb_occupation.Text = customer.occupation;
            this.tb_jobDuties.Text = customer.jobDuties;
            this.tb_employer.Text = customer.employer;
            this.tb_noOfYears.Text = Convert.ToString(customer.noOfYears);
            this.tb_industry.Text = customer.industry;
            this.tb_annualEarnedIncome.Text = Convert.ToDecimal(customer.annualIncomeCurrent).ToString("###,###.00");
            this.tb_annualEarnedIncomePrevious.Text = Convert.ToDecimal(customer.annualIncomePrevious).ToString("###,###.00");

            if (!"".Equals(customer.otherIncomeSource))
            {
                rb_yesOtherIncome.Checked = true;
                rb_noOtherIncome.Checked = false;
            }
            else
            {
                rb_yesOtherIncome.Checked = false;
                rb_noOtherIncome.Checked = true;
            }

            this.tb_otherIncomeSource.Text = customer.otherIncomeSource;
            this.tb_otherAnnualEarnedIncome.Text = Convert.ToDecimal(customer.otherSourceIncome).ToString("###,###.00");
            this.tb_sourceOfWealth.Text = customer.sourceOfWealth;

            if ("Y".Equals(customer.bankruptcy))
            {
                rb_yesBankruptcy.Checked = true;
                rb_noBankruptcy.Checked = false;
            }
            else
            {
                rb_yesBankruptcy.Checked = false;
                rb_noBankruptcy.Checked = true;
            }

            //Plan Details
            this.tb_planName.Text = "Flexi Protect";
            this.tb_basicSumInsured.Text = policy.basicSumInsured.ToString("###,###.00");

            int age = -1;
            age = Util.getAge(customer.dob);
            if (age >= 0)
            {
                this.tb_basicPlanDuration.Text = Convert.ToString(99 - age) + " Years";
            }

            if (customer.plusRiderSumInsured != 0)
            {
                rb_plusRiderYes.Checked = true;
                rb_plusRiderNo.Checked = false;
                this.tb_plusRiderDuration.Text = Convert.ToString(65 - age) + " Years";
                this.tb_plusRiderSumInsured.Text = policy.plusRiderSumInsured.ToString("###,###.00");
            }
            else
            {
                rb_plusRiderYes.Checked = false;
                rb_plusRiderNo.Checked = true;
            }

            if (customer.waiverRiderPremium != 0)
            {
                rb_waiverRiderYes.Checked = true;
                rb_waiverRiderNo.Checked = false;
                this.tb_waiverRiderDuration.Text = Convert.ToString(65 - age) + " Years";
            }
            else
            {
                rb_waiverRiderYes.Checked = false;
                rb_waiverRiderNo.Checked = true;
            }

            this.tb_totalAnnualPremium.Text = policy.totalAnnualPremium.ToString("###,###.00");


            //Fund Allocation
            List<Fund> choosenFundAllocation = Fund.getActivePortfolioFunds(policy.policyNo);

            this.gv_choosenFundAllocation.DataSource = choosenFundAllocation;
            this.gv_choosenFundAllocation.DataBind();

            //Payment Details
            if ("M".Equals(policy.premiumFrequency))
            {
                rb_monthly.Checked = true;
                rb_semiAnnually.Checked = false;
                rb_annually.Checked = false;
            }
            else if ("S".Equals(policy.premiumFrequency))
            {
                rb_monthly.Checked = false;
                rb_semiAnnually.Checked = true;
                rb_annually.Checked = false;
            }
            else
            {
                rb_monthly.Checked = false;
                rb_semiAnnually.Checked = false;
                rb_annually.Checked = true;
            }

            if ("Auto".Equals(policy.paymentMethod))
            {
                rb_auto.Checked = true;
                rb_direct.Checked = false;
            }
            else
            {
                rb_auto.Checked = false;
                rb_direct.Checked = true;
            }

            UserAccount userAccount = UserAccount.getUserAccountByAccountId(policy.accountId);
            this.tb_accountNumber.Text = userAccount.branchCode + "-" + userAccount.accountNumber + "-" + userAccount.accountTypeCode;
            this.tb_accountType.Text = userAccount.accountType;
            this.tb_currency.Text = userAccount.currency;

        }
    }


    protected void btn_editPersonalDetails_Click(object sender, ImageClickEventArgs e)
    {
        //Enable TextFields before edit
        this.tb_firstName.Enabled = true;
        this.tb_lastName.Enabled = true;
        this.tb_nric.Enabled = true;
        this.tb_nationality.Enabled = true;
        this.tb_dob.Enabled = true;
        this.rb_male.Enabled = true;
        this.rb_female.Enabled = true;
        this.tb_maritalStatus.Enabled = true;
        this.rb_smoker.Enabled = true;
        this.rb_nonSmoker.Enabled = true;
        this.tb_mailingAddress.Enabled = true;
        this.tb_postalCode.Enabled = true;
        this.tb_mobileNo.Enabled = true;
        this.tb_telNo.Enabled = true;
        this.tb_email.Enabled = true;
        this.btn_okPersonalDetails.Visible = true;
        this.btn_cancelPersonalDetails.Visible = true;
    }
    protected void btn_okPersonalDetails_Click(object sender, ImageClickEventArgs e)
    {
        //Personal Details
        String policyNo = this.policyNo.Value;
        Policy policy = Policy.getPolicy(policyNo);
        Customer customer = Customer.getCustomerInfo(policy.userId);

        customer.firstName = this.tb_firstName.Text;
        customer.lastName = this.tb_lastName.Text;
        customer.nric = this.tb_nric.Text;
        customer.nationality = this.tb_nationality.Text;
        customer.maritalStatus = this.tb_maritalStatus.Text;

        if (this.tb_dob.Text != null && !this.tb_dob.Text.Equals(""))
        {
            //customer.dob = Convert.ToDateTime(this.tb_dob.Text);
            customer.dob = DateTime.ParseExact(this.tb_dob.Text, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
        }
        else
        {
            customer.dob = DateTime.Now;
        }

        if (this.rb_male.Checked == true)
        {

            customer.gender = "Male";
        }
        else
        {
            customer.gender = "Female";
        }

        customer.maritalStatus = this.tb_maritalStatus.Text;

        if (rb_smoker.Checked == true)
        {
            customer.smoker = "Y";
        }
        else
        {
            customer.smoker = "N";
        }

        //Contact Details
        customer.mailingAddress = this.tb_mailingAddress.Text;
        customer.postalCode = this.tb_postalCode.Text;
        customer.mobileNo = this.tb_mobileNo.Text;
        customer.telNo = this.tb_telNo.Text;
        customer.email = this.tb_email.Text; 

        if (Session["userID"] != null)
        {
            policy.staffId = Convert.ToInt32(Session["userID"]);
        }
        else
        {
            policy.staffId = 0;
        }

        customer.updateCustomerInfo();

        this.btn_okPersonalDetails.Visible = false;
        this.btn_cancelPersonalDetails.Visible = false;

        this.tb_firstName.Enabled = false;
        this.tb_lastName.Enabled = false;
        this.tb_nric.Enabled = false;
        this.tb_nationality.Enabled = false;
        this.tb_dob.Enabled = false;
        this.rb_male.Enabled = false;
        this.rb_female.Enabled = false;
        this.tb_maritalStatus.Enabled = false;
        this.rb_smoker.Enabled = false;
        this.rb_nonSmoker.Enabled = false;
        this.tb_mailingAddress.Enabled = false;
        this.tb_postalCode.Enabled = false;
        this.tb_mobileNo.Enabled = false;
        this.tb_telNo.Enabled = false;
        this.tb_email.Enabled = false;
    }
    protected void btn_cancelPersonalDetails_Click(object sender, ImageClickEventArgs e)
    {
        //Disable TextFields on cancel edit
        this.tb_firstName.Enabled = false;
        this.tb_lastName.Enabled = false;
        this.tb_nric.Enabled = false;
        this.tb_nationality.Enabled = false;
        this.tb_dob.Enabled = false;
        this.rb_male.Enabled = false;
        this.rb_female.Enabled = false;
        this.tb_maritalStatus.Enabled = false;
        this.rb_smoker.Enabled = false;
        this.rb_nonSmoker.Enabled = false;
        this.tb_mailingAddress.Enabled = false;
        this.tb_postalCode.Enabled = false;
        this.tb_mobileNo.Enabled = false;
        this.tb_telNo.Enabled = false;
        this.tb_email.Enabled = false;

        this.btn_okPersonalDetails.Visible = false;
        this.btn_cancelPersonalDetails.Visible = false;
    }

    protected void btn_editOccupationalDetails_Click(object sender, ImageClickEventArgs e)
    {
        this.tb_occupation.Enabled = true;
        this.tb_jobDuties.Enabled = true;
        this.tb_employer.Enabled = true;
        this.tb_noOfYears.Enabled = true;
        this.tb_industry.Enabled = true;
        this.tb_annualEarnedIncome.Enabled = true;
        this.tb_annualEarnedIncomePrevious.Enabled = true;
        this.rb_yesOtherIncome.Enabled = true;
        this.rb_noOtherIncome.Enabled = true;
        this.tb_otherIncomeSource.Enabled = true;
        this.tb_otherAnnualEarnedIncome.Enabled = true;
        this.tb_sourceOfWealth.Enabled = true;
        this.rb_yesBankruptcy.Enabled = true;
        this.rb_noBankruptcy.Enabled = true;

        this.btn_okOccupationalDetails.Visible = true;
        this.btn_cancelOccupationalDetails.Visible = true;
    }
    protected void btn_okOccupationalDetails_Click(object sender, ImageClickEventArgs e)
    {

        String policyNo = this.policyNo.Value;
        Policy policy = Policy.getPolicy(policyNo);
        Customer customer = Customer.getCustomerInfo(policy.userId);

        //Occupational Details
        customer.occupation = this.tb_occupation.Text;
        customer.jobDuties = this.tb_jobDuties.Text;
        customer.employer = this.tb_employer.Text;

        if (this.tb_noOfYears.Text != null && !this.tb_noOfYears.Text.Equals(""))
        {
            customer.noOfYears = Convert.ToDecimal(this.tb_noOfYears.Text);
        }
        else
        {
            customer.noOfYears = null;
        }
        customer.industry = this.tb_industry.Text;


        //Income Details
        if (this.tb_annualEarnedIncome.Text != null && !this.tb_annualEarnedIncome.Text.Equals(""))
        {
            customer.annualIncomeCurrent = Convert.ToDecimal(this.tb_annualEarnedIncome.Text);
        }
        else
        {
            customer.annualIncomeCurrent = 0;
        }

        if (tb_annualEarnedIncomePrevious.Text != null && !this.tb_annualEarnedIncomePrevious.Text.Equals(""))
        {
            customer.annualIncomePrevious = Convert.ToDecimal(this.tb_annualEarnedIncomePrevious.Text);
        }
        else
        {
            customer.annualIncomePrevious = 0;
        }

        customer.otherIncomeSource = this.tb_otherIncomeSource.Text;

        if (this.tb_otherAnnualEarnedIncome.Text != null && !this.tb_otherAnnualEarnedIncome.Text.Equals(""))
        {
            customer.otherSourceIncome = Convert.ToDecimal(this.tb_otherAnnualEarnedIncome.Text);
        }
        else
        {
            customer.otherSourceIncome = null;
        }

        customer.sourceOfWealth = this.tb_sourceOfWealth.Text;

        if (rb_yesBankruptcy.Checked == true)
        {
            customer.bankruptcy = "Y";
        }
        else
        {
            customer.bankruptcy = "N";
        }

        customer.updateCustomerInfo();

        this.btn_okOccupationalDetails.Visible = false;
        this.btn_cancelOccupationalDetails.Visible = false;

        this.tb_occupation.Enabled = false;
        this.tb_jobDuties.Enabled = false;
        this.tb_employer.Enabled = false;
        this.tb_noOfYears.Enabled = false;
        this.tb_industry.Enabled = false;
        this.tb_annualEarnedIncome.Enabled = false;
        this.tb_annualEarnedIncomePrevious.Enabled = false;
        this.rb_yesOtherIncome.Enabled = false;
        this.rb_noOtherIncome.Enabled = false;
        this.tb_otherIncomeSource.Enabled = false;
        this.tb_otherAnnualEarnedIncome.Enabled = false;
        this.tb_sourceOfWealth.Enabled = false;
        this.rb_yesBankruptcy.Enabled = false;
        this.rb_noBankruptcy.Enabled = false;
    }
    protected void btn_cancelOccupationalDetails_Click(object sender, ImageClickEventArgs e)
    {
        this.tb_occupation.Enabled = false;
        this.tb_jobDuties.Enabled = false;
        this.tb_employer.Enabled = false;
        this.tb_noOfYears.Enabled = false;
        this.tb_industry.Enabled = false;
        this.tb_annualEarnedIncome.Enabled = false;
        this.tb_annualEarnedIncomePrevious.Enabled = false;
        this.rb_yesOtherIncome.Enabled = false;
        this.rb_noOtherIncome.Enabled = false;
        this.tb_otherIncomeSource.Enabled = false;
        this.tb_otherAnnualEarnedIncome.Enabled = false;
        this.tb_sourceOfWealth.Enabled = false;
        this.rb_yesBankruptcy.Enabled = false;
        this.rb_noBankruptcy.Enabled = false;

        this.btn_okOccupationalDetails.Visible = false;
        this.btn_cancelOccupationalDetails.Visible = false;
    }

    protected void btn_editPlanDetails_Click(object sender, ImageClickEventArgs e)
    {
        this.tb_planName.Enabled = true;
        this.tb_basicSumInsured.Enabled = true;
        this.tb_basicPlanDuration.Enabled = true;
        this.rb_plusRiderYes.Enabled = true;
        this.rb_plusRiderNo.Enabled = true;
        this.tb_plusRiderDuration.Enabled = true;
        this.tb_plusRiderSumInsured.Enabled = true;
        this.rb_waiverRiderYes.Enabled = true;
        this.rb_waiverRiderNo.Enabled = true;
        this.tb_waiverRiderDuration.Enabled = true;
        this.tb_totalAnnualPremium.Enabled = true;

        this.btn_okPolicyDetails.Visible = true;
        this.btn_cancelPolicyDetails.Visible = true;
    }
    protected void btn_okPolicyDetails_Click(object sender, ImageClickEventArgs e)
    {
        String policyNo = this.policyNo.Value;
        Policy policy = Policy.getPolicy(policyNo);
        Customer customer = Customer.getCustomerInfo(policy.userId);

        //Plan Details
        policy.basicSumInsured = Convert.ToDecimal(this.tb_basicSumInsured.Text);

        if (this.tb_plusRiderSumInsured.Text != null && !this.tb_plusRiderSumInsured.Text.Equals(""))
        {
            policy.plusRiderSumInsured = Convert.ToDecimal(this.tb_plusRiderSumInsured.Text);
        }
        else
        {
            policy.plusRiderSumInsured = 0;
        }
        if (this.tb_plusRiderSumInsured.Text != null && !this.tb_plusRiderSumInsured.Text.Equals(""))
        {
            policy.waiverRiderPremium = policyRules.getAnnualWaiverRiderPremiumRate(Util.getAge(DateTime.ParseExact(this.tb_dob.Text, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture)), customer.gender, customer.smoker) * Convert.ToDecimal(this.tb_plusRiderSumInsured.Text) / 1000;
        }
        else
        {
            policy.waiverRiderPremium = 0;
        }
        policy.totalAnnualPremium = Convert.ToDecimal(this.tb_totalAnnualPremium.Text);
        policy.benefitType = "The Higher of the Sum Insured Or Account Value";

        policy.updatePolicy(policyNo);

        this.btn_okPolicyDetails.Visible = false;
        this.btn_cancelPolicyDetails.Visible = false;

        this.tb_planName.Enabled = false;
        this.tb_basicSumInsured.Enabled = false;
        this.tb_basicPlanDuration.Enabled = false;
        this.rb_plusRiderYes.Enabled = false;
        this.rb_plusRiderNo.Enabled = false;
        this.tb_plusRiderDuration.Enabled = false;
        this.tb_plusRiderSumInsured.Enabled = false;
        this.rb_waiverRiderYes.Enabled = true;
        this.rb_waiverRiderNo.Enabled = false;
        this.tb_waiverRiderDuration.Enabled = false;
        this.tb_totalAnnualPremium.Enabled = false;
    }
    protected void btn_cancelPolicyDetails_Click(object sender, ImageClickEventArgs e)
    {
        this.tb_planName.Enabled = false;
        this.tb_basicSumInsured.Enabled = false;
        this.tb_basicPlanDuration.Enabled = false;
        this.rb_plusRiderYes.Enabled = false;
        this.rb_plusRiderNo.Enabled = false;
        this.tb_plusRiderDuration.Enabled = false;
        this.tb_plusRiderSumInsured.Enabled = false;
        this.rb_waiverRiderYes.Enabled = true;
        this.rb_waiverRiderNo.Enabled = false;
        this.tb_waiverRiderDuration.Enabled = false;
        this.tb_totalAnnualPremium.Enabled = false;

        this.btn_okPolicyDetails.Visible = false;
        this.btn_cancelPolicyDetails.Visible = false;
    }

    protected void btn_editPaymentDetails_Click(object sender, ImageClickEventArgs e)
    {
        this.rb_monthly.Enabled = true;
        this.rb_semiAnnually.Enabled = true;
        this.rb_annually.Enabled = true;
        this.rb_auto.Enabled = true;
        this.rb_direct.Enabled = true;
        this.tb_accountNumber.Enabled = true;
        this.tb_accountType.Enabled = true;
        this.tb_currency.Enabled = true;

        this.btn_okPaymentDetails.Visible = true;
        this.btn_cancelPaymentDetails.Visible = true;
    }
    protected void btn_okPaymentDetails_Click(object sender, ImageClickEventArgs e)
    {
        String policyNo = this.policyNo.Value;
        Policy policy = Policy.getPolicy(policyNo);
        Customer customer = Customer.getCustomerInfo(policy.userId);

        if (rb_monthly.Checked == true)
        {
            policy.premiumFrequency = "M";
        }
        else if (rb_semiAnnually.Checked == true)
        {
            policy.premiumFrequency = "S";
        }
        else
        {
            policy.premiumFrequency = "A";
        }

        if (rb_auto.Checked == true)
        {
            policy.paymentMethod = "Auto";
        }
        else
        {
            policy.paymentMethod = "Direct";
        }


        UserAccount userAccount = UserAccount.getUserAccountByNumber(this.tb_accountNumber.Text);
        policy.accountId = userAccount.accountID;

        policy.updatePolicy(policyNo);

        this.btn_okPaymentDetails.Visible = false;
        this.btn_cancelPaymentDetails.Visible = false;

        this.rb_monthly.Enabled = false;
        this.rb_semiAnnually.Enabled = false;
        this.rb_annually.Enabled = false;
        this.rb_auto.Enabled = false;
        this.rb_direct.Enabled = false;
        this.tb_accountNumber.Enabled = false;
        this.tb_accountType.Enabled = false;
        this.tb_currency.Enabled = false;
    }
    protected void btn_cancelPaymentDetails_Click(object sender, ImageClickEventArgs e)
    {
        this.rb_monthly.Enabled = false;
        this.rb_semiAnnually.Enabled = false;
        this.rb_annually.Enabled = false;
        this.rb_auto.Enabled = false;
        this.rb_direct.Enabled = false;
        this.tb_accountNumber.Enabled = false;
        this.tb_accountType.Enabled = false;
        this.tb_currency.Enabled = false;

        this.btn_okPaymentDetails.Visible = false;
        this.btn_cancelPaymentDetails.Visible = false;
    }
}